<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Action
{
	public static function dbActions($module, $controller, $returnArray = false)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		$actions = $actionrDao->dbActions($module, $controller);
		if ($returnArray) {
			$return = array();
			if ($actions) {
				foreach ($actions as $index => $action) {
					$return[] = $action['action_id'];
				}
			}
			return $return;
		}
		return $actions;
	}

	public static function getByMCA($module, $controller, $action)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		return $actionrDao->getByMCA($module, $controller, $action);
	}
	
	public static function getDbActionsByModule($module)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		$actions = $actionrDao->getDbActionsByModule($module);
		$return = array();
		$controller = null;
		for ($i = 0; $i < count($actions); $i++) {
			if ($controller != $actions[$i]['controller_id']) {
				if ($controller != null) {
					$return[] = $item;
				}
				$controller = $actions[$i]['controller_id'];
				$item = array();
				$item['controller_id'] = $controller;
				$item['module_id'] = $actions[$i]['module_id'];
				$item['actions'] 	   = array();
			}
			$item['actions'][] = array(
								'action_id'		=> $actions[$i]['action_id'],
							);
			if ($i == count($actions) - 1 && $controller == $actions[$i]['controller_id']) {
				$return[] = $item;
			}		
		}
		return $return;
	}
	
	public static function add($action)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		return $actionrDao->add($action);
	}
	
	public static function delete($module, $controller, $action)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		return $actionrDao->delete($module, $controller, $action);
	}
	
	public static function deleteByController($module, $controller)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$actionrDao = new Modules_Core_Models_Mysql_Action();
		$actionrDao->setConnection($conn);
		return $actionrDao->deleteByController($module, $controller);
	}
}